package org.csu.newpetstoreback.service.impl;

import com.alipay.api.domain.Keyword;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.newpetstoreback.common.CommonResponse;
import org.csu.newpetstoreback.entity.Category;
import org.csu.newpetstoreback.entity.Item;
import org.csu.newpetstoreback.entity.ItemInventory;
import org.csu.newpetstoreback.entity.Product;
import org.csu.newpetstoreback.persistence.*;
import org.csu.newpetstoreback.service.CatalogService;
import org.csu.newpetstoreback.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("catalogService")
public class CatalogServiceImpl implements CatalogService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemInventoryMapper itemInventoryMapper;
    @Autowired
    private FindProductMapper findProductMapper;


    @Override
    public CommonResponse<List<Category>> getCategories() {
        List<Category> categoryList = categoryMapper.selectList(null);
        if(categoryList.isEmpty()){
            return CommonResponse.createForNotFound("没有分类信息");
        }
        return CommonResponse.createForSuccess(categoryList);
    }

    @Override
    public CommonResponse<Category> getCategoryById(String categoryId) {
       Category category =  categoryMapper.selectById(categoryId);
       if(category==null){
           return CommonResponse.createForNotFound("没有该ID对应的Category");
       }
        return CommonResponse.createForSuccess(category);
    }

    @Override
    public CommonResponse<List<Product>> getProductsByCategory(String categoryId) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("categoryid",categoryId);//比对数据库Product表中category列与categoryId是否一致

        List<Product> productList = productMapper.selectList(queryWrapper);//比中则找到其对应Product子类列表
        if(productList.isEmpty()){
            return CommonResponse.createForNotFound("该分类下没有Product子分类");
        }
        return CommonResponse.createForSuccess(productList);
    }

    @Override
    public CommonResponse<Product> getProductById(String productId) {
        Product product =  productMapper.selectById(productId);
        if(product==null){
            return CommonResponse.createForNotFound("没有该ID对应的Product");
        }
        return CommonResponse.createForSuccess(product);
    }

    @Override
    public CommonResponse<List<ItemVO>> getItemsByProductId(String productId) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid",productId);
        List<Item> itemList = itemMapper.selectList(queryWrapper);
        if(itemList.isEmpty()){
            return CommonResponse.createForNotFound("该分类下没有Item子分类");
        }

        Product product = productMapper.selectById(productId);
        List<ItemVO> itemVOList = new ArrayList<>();
        for(Item item:itemList){
            ItemVO itemVO = itemToItemVO(item,product);
            itemVOList.add(itemVO);
        }
        return CommonResponse.createForSuccess(itemVOList);


    }

    private ItemVO itemToItemVO(Item item,Product product){
        ItemVO itemVO = new ItemVO();
        itemVO.setItemId(item.getItemId());
        itemVO.setProductId(item.getProductId());
        itemVO.setCategoryId(item.getCategoryId());
        itemVO.setListPrice(item.getListPrice());
        itemVO.setUnitCost(item.getUnitCost());
        itemVO.setSupplierId(item.getSupplierId());
        itemVO.setStatus(item.getStatus());
        itemVO.setAttribute1(item.getAttribute1());
        itemVO.setAttribute2(item.getAttribute2());
        itemVO.setAttribute3(item.getAttribute3());
        itemVO.setIImage(item.getIImage());
        itemVO.setIName(item.getIName());
        itemVO.setIDescn(item.getIDescn());

        itemVO.setCategoryId(product.getCategoryId());
        itemVO.setProductName(product.getPName());
        itemVO.setProductDescription(product.getPDescn());


        ItemInventory itemInventory = itemInventoryMapper.selectById(item.getItemId());
        itemVO.setQuantity(itemInventory.getQuantity());

        return itemVO;
    }
    @Override
    public CommonResponse<ItemVO> getItemById(String itemId) {
        Item item =  itemMapper.selectById(itemId);
        if(item==null){
            return CommonResponse.createForNotFound("没有该ID对应的Item");
        }
        Product product = productMapper.selectById(item.getProductId());
        ItemVO itemVO = itemToItemVO(item,product);
        return CommonResponse.createForSuccess(itemVO);

    }

    @Override
    public CommonResponse<List<Product>> searchProductList(String keyword) {

        return CommonResponse.createForSuccess(findProductMapper.searchProductList("%"+keyword.toLowerCase()+"%"));
    }
}
